<template>
  <div class="bid-info">
      <h3>
        设置评标岗位
        <!-- <el-button size="mini" type="primary" @click="saveEvaStation(0)" v-if="setExpertStatus===0">暂存</el-button> -->
        <el-button size="mini" type="primary" @click="saveEvaStation(1)" :disabled="!status" v-if="setExpertStatus===0">提交</el-button>
      </h3>
      <div class="info-div">
          <div class="info-title">
            <span>技术岗位</span>
            <el-button size="mini" type="primary" @click="addExpertMan(1)"  v-if="setExpertStatus===0">增加</el-button>
          </div>
          <el-table :data="expertMan1" border>
            <el-table-column label="岗位ID" align="center" prop="sysStationId" width="100px"></el-table-column>
            <el-table-column label="岗位" align="center" prop="sysStationName"></el-table-column>
            <el-table-column label="公司" align="center" prop="companyName"></el-table-column>
            <el-table-column label="部门" align="center" prop="deptName"></el-table-column>           
            <el-table-column label="评标状态" align="center" prop="evaluationStatus" width="100px">
                <template slot-scope="scope">
                    <TableIcon :flag="scope.row.evaluationStatus"
                    :titles="[{status:0,title:'未评标'},{status:1,title:'已评标'}]"></TableIcon>
                </template>
            </el-table-column>
            <!-- <el-table-column label="评分" align="center" prop="stockUnit"></el-table-column>
            <el-table-column label="评分表" align="center" prop="qualityTime"></el-table-column> -->
            <el-table-column label="操作" align="center" prop="planQty">
                <template slot-scope="scope" v-if="setExpertStatus===0">
                    <el-button type="text" @click="expertMan1.splice(scope.row.index,1)">删除</el-button>
                </template>
            </el-table-column>
            </el-table>
        <div class="info-title mt20">
            <span>商务岗位</span>
            <el-button size="mini" type="primary" @click="addExpertMan(2)"  v-if="setExpertStatus===0">增加</el-button>
          </div>
          <el-table :data="expertMan2" border>
            <el-table-column label="岗位ID" align="center" prop="sysStationId" width="100px"></el-table-column>
            <el-table-column label="岗位" align="center" prop="sysStationName"></el-table-column>
            <el-table-column label="公司" align="center" prop="companyName"></el-table-column>
            <el-table-column label="部门" align="center" prop="deptName"></el-table-column>           
            <el-table-column label="评标状态" align="center" prop="evaluationStatus" width="100px">
                <template slot-scope="scope">
                    <TableIcon :flag="scope.row.evaluationStatus"
                    :titles="[{status:0,title:'未评标'},{status:1,title:'已评标'}]"></TableIcon>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" prop="planQty">
                <template slot-scope="scope" v-if="setExpertStatus===0">
                    <el-button type="text" @click="expertMan2.splice(scope.row.index,1)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- <div class="info-text w50 mt20">
            <span class="text-label">
                商务评分模板：
            </span>
            <span class="text-value">
                <el-upload class="upload-file" ref="upload" :action="this.lib.config.host.system + '/common/fileupload/upload'" :data=fileData
                :limit="1" :on-success="res=>{uploadSuccess(res,'businessFileId')}">
                <el-button slot="trigger" type="primary">选取文件</el-button>
                </el-upload>
            </span>
        </div> -->
        <!-- <div class="info-text w50 mt20">
            <span class="text-label">
                技术评分模板：
            </span>
            <span class="text-value">
                <el-upload class="upload-file" ref="upload" :action="this.lib.config.host.system + '/common/fileupload/upload'" :data=fileData
                :limit="1" :on-success="res=>{uploadSuccess(res,'techFileId')}">
                <el-button slot="trigger" type="primary">选取文件</el-button>
                </el-upload>
            </span>
        </div> -->
      </div>
      <div class="info-div" v-if="submitCompStatus===0">
          <div class="info-title">
              <span>开标</span>
          </div>
          <div class="info-text w100">
            <el-input style="width:25%" v-model="secretKey"></el-input>
            <el-button type="primary" @click="openBid" :disabled="!secretKey">确认开标</el-button>
            <el-button @click="showPerson">获取开标秘钥</el-button>{{infoData.secretKeyValue}}
          </div>
      </div>
      <div class="info-div" v-if="submitCompStatus===1">
          <div class="info-title">
              <span>报价汇总表</span>
          </div>
        <el-table :data="quoteSumDate" border>
            <el-table-column label="供应商编码" align="center" prop="vendorCode" width="100px"></el-table-column>
            <el-table-column label="供应商名称" align="center" prop="vendorName"></el-table-column>
            <el-table-column label="投标总价" align="center" prop="totalPrice"></el-table-column>
            <el-table-column label="资质文件" align="center" prop="aptitudeFileName">
                <template slot-scope="scope">
                    <a href="javascript:;" @click="downloadFile(scope.row.aptitudeFileName,scope.row.aptitudeFilePath)" class="download_color">{{scope.row.aptitudeFileName}}</a>
                </template>
            </el-table-column>
            <el-table-column label="商务方案" align="center" prop="businessFileName">
                <template slot-scope="scope">
                    <a href="javascript:;" @click="downloadFile(scope.row.businessFileName,scope.row.businessFilePath)" class="download_color">{{scope.row.businessFileName}}</a>
                </template>
            </el-table-column>
            <el-table-column label="技术方案" align="center" prop="techPlanFileName">
                <template slot-scope="scope">
                    <a href="javascript:;" @click="downloadFile(scope.row.techFileName,scope.row.techFilePath)" class="download_color">{{scope.row.techFileName}}</a>
                </template>
            </el-table-column>
        </el-table>
      </div>
      <div class="info-div" v-if="submitCompStatus===1">
          <div class="info-title">
              <span>报价明细表</span>
          </div>
        <el-table :data="quoteDetailData" border :span-method="arraySpanMethod">
            <el-table-column label="产品编码" align="center" prop="materialCode"></el-table-column>
            <el-table-column label="产品名称" align="center" prop="materialName"></el-table-column>
            <el-table-column label="规格型号" align="center" prop="materialSpec"></el-table-column>
            <el-table-column label="单位" align="center" prop="materialUnit"></el-table-column>
            <el-table-column label="数量" align="center" prop="planQty"></el-table-column>
            <el-table-column label="供货区域" align="center" prop="purOrgName"></el-table-column>
            <el-table-column label="工厂" align="center" prop="factoryName"></el-table-column>
            <el-table-column label="供应商" align="center" prop="vendorName" width="220px"></el-table-column>
            <el-table-column label="含税单价" align="center" prop="lastTaxPrice"></el-table-column>
            <el-table-column label="含税总价" align="center" prop="baseTotalPrice"></el-table-column>
            <el-table-column label="税率" align="center" prop="dutyRate"></el-table-column>
            <el-table-column label="质保期" align="center" prop="qualityTime" width="100px"></el-table-column>
            <el-table-column label="交货期" align="center" prop="deliveryDate" width="100px"></el-table-column>
        </el-table>
      </div>
      <!-- <div class="info-div">
          <div class="info-title">
              <span>投标文件</span>
          </div>
        <el-table :data="quoteSumDate" border>
            <el-table-column label="供应商" align="center" prop="vendorCode"></el-table-column>
            <el-table-column label="资质文件" align="center" prop="vendorName"></el-table-column>
            <el-table-column label="商务方案" align="center" prop="vendorAddress"></el-table-column>
            <el-table-column label="技术方案" align="center" prop="vendorAddress"></el-table-column>
        </el-table>
      </div> -->

      <el-dialog :title="'开标秘钥'" :visible.sync="checkVisible" class="dialog-form" width="500px">
        <div class="info-text w100">
            <span>
                秘钥接收人：
            </span>
            <span>
                <el-select v-model="userId" placeholder="请选择">
                    <el-option v-for="item in leaderUsers" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </span>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="sendSecretKey" :disabled="!userId">确定</el-button>
        </div>
    </el-dialog>

    <el-dialog :title="addExpertTitle" :visible.sync="addExpertDialog" class="dialog-form" width="800px">
        <div class="info-text w100">
            <bidVendors ref="trans"
                @transAdd="addTrans" width="100%" :checkName="['sysStationName']"
                :title="{left:'可选岗位',right:'已选岗位'}"
                :searchForm="searchForm" :selectData="addExpertData"
                :isQsparams="false"
                :url="lib.config.host.masterData + '/system/expert/query'">
            </bidVendors>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="saveExpert" :disabled="!addExpertData.length">保存</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
  import qs from 'qs';
  import bidVendors from './experts.vue'
  export default {
    props:['stepData'],
    components: {
      bidVendors
    },
    computed:{
        setExpertStatus(){
            return this.stepData.bidMainDto.setExpertStatus===3?0:1;
        },
        submitCompStatus(){
            return this.stepData.bidMainDto.submitCompStatus===3?0:1;
        },
        status(){
            let openStatus= this.stepData.bidMainDto.setExpertStatus === 3;
            let dataStatue=Object.keys(this.ruleForm).every(k=>{return !!this.ruleForm[k]});
            return openStatus && dataStatue
        }
    },
    data() {
      return {
          // 定义附件的业务模块和业务动作
          fileData: {
            busAction: "BID_SET_EVA_METHOD",
            tableName: "CT_PS_BID_MAIN_EXT",
            tableId: this.$route.query.id
          },

          addExpertTitle:'',
          secretKey:'',
          userId:'',
          addExpertDialog:false,
          checkVisible:false,
          searchForm:[],
          addExpertData :[],
          quoteDetailData:[],
          quoteSumDate:[],
          leaderUsers:[],
          expertMan1:[],
          expertMan2:[], 
          infoData:{},
          npcTaskForm:{},
          ruleForm:{            
            //businessFileId:'',
            //techFileId:'',
            evaUserList:[]
          }
      };
    },
    mounted(){
        this.infoData=this.stepData.bidMainDto;
    },
    created(){
       this.getqueryEvaStation();
       this.getList();
    },
    methods: {
        uploadSuccess: function (response,type) {
            //附件上传成功回调
            this.ruleForm[type] = response.id;
        },
        addTrans(data){

        },
        getqueryEvaStation(){
            //获取技术评标人
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/queryEvaStation/${this.$attrs.data.id}/1`).then((res) => {
                this.expertMan1=res.data;
            })
            .catch(function (error) {
                console.log(error);
            });
            //获取商务评标人
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/queryEvaStation/${this.$attrs.data.id}/2`).then((res) => {
                this.expertMan2=res.data;
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        getList(){
            //获取开标秘钥人
            this.axios.post(`${this.lib.config.host.system}/system/usermgt/users/findByRoleCode/keyRecipients`).then((res) => {
                if(res.data){
                    this.leaderUsers = res.data;
                }
            })
            .catch(function (error) {
                console.log(error);
            });
            //获取报价明细表
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/queryVendorMaterial/${this.$attrs.data.id}`).then((res) => {
                if(res.data){
                    this.quoteDetailData = res.data;
                }
            }).catch(function (error) {
                console.log(error);
            });
            //获取报价汇总表
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/getAllQuote/${this.$attrs.data.id}`).then((res) => {
                this.quoteSumDate=res.data;
            }).catch(function (error) {
                console.log(error);
            });
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            //合并单元格方法，第一个if为前4个单元格维度，标识为materialId
            if (columnIndex === 0||columnIndex === 1||columnIndex === 2||columnIndex === 3) {
                let col=0;
                this.quoteDetailData.forEach(item=>{
                    if(item.materialId===row.materialId){
                        col++;
                    }
                })
                let index=this.quoteDetailData.findIndex(item=>{return item.materialId===row.materialId});
                if (rowIndex===index) {
                    return {
                        rowspan: col,
                        colspan: 1
                    };
                }else{
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
            }
            //合并单元格方法，第二个if为后3个单元格维度，标识为factoryId
            if (columnIndex === 4||columnIndex === 5||columnIndex === 6) {
                let col=0;
                this.quoteDetailData.forEach(item=>{
                    if(item.factoryId===row.factoryId){
                        col++;
                    }
                })
                let index=this.quoteDetailData.findIndex(item=>{return item.factoryId===row.factoryId});
                if (rowIndex===index) {
                    return {
                        rowspan: col,
                        colspan: 1
                    };
                }else{
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
            }
        },
        showPerson(){
            this.userId='';
            this.checkVisible=true;
        },
        sendSecretKey(){
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bid/sendSecretKey/${this.$attrs.data.id}/${this.userId}`).then((res) => {
                if(res.data.success){
                    this.$message.success('秘钥发送成功！');
                    this.checkVisible=false;
                }
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        openBid(){
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bid/openBid/${this.$attrs.data.id}/${this.secretKey}`).then((res) => {
                if(res.data.success){
                    if(res.data.message==='KEY_WRONG'){
                        this.$message.info('秘钥错误！');
                    }else{
                        this.$message.success('已开标！');
                    }
                }
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        addExpertMan(type){
            //新增评标人
            this.addExpertTitle=type===1?'新增技术评标岗位':'新增商务评标岗位';
            this.addType=type;
            this.addExpertData=[];
            this.addExpertDialog=true;
            this.$nextTick(()=>{
              this.$refs.trans.resetCheck();
            })
        },
        saveExpert(){
            let key=`expertMan${this.addType}`;
            let check=false;
            this.addExpertData.forEach(val=>{
                if(this[key].some(item=>{return val.id===item.id})){
                    check=true;
                }
            })
            if(check){
                this.$message.info('不可重复添加！');
                return;
            }
            this.addExpertData.forEach(val=>{
                this[key].push({...val,evaluationType:this.addType})
            })
            this.addExpertDialog=false;
        },
        saveEvaStation(type){
            let data={
                //:this.ruleForm.businessFileId,
                //techFileId:this.ruleForm.techFileId,
                evaUserList:[...this.expertMan1,...this.expertMan2]
            }
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/evaluation/saveEvaStation/${this.$attrs.data.id}/${type}`,data).then((res) => {
                if(res.data.success){
                    this.$emit('initList');
                    this.getqueryEvaStation();
                    this.getList();
                    this.$message.success(type===1?'提交成功！':'暂存成功！');
                }
            }).catch(function (error) {
                console.log(error);
            });
        }
    }
  }

</script>
